package com.tencent.miniproject.ui;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tencent.miniproject.R;
import com.tencent.miniproject.adapter.DetailCommentsAdapter;
import com.tencent.miniproject.base.BaseActivity;
import com.tencent.miniproject.been.ContentDetail;
import com.tencent.miniproject.been.DetailComment;
import com.tencent.miniproject.presenter.DetailPresenter;
import com.tencent.miniproject.presenter.i.IDetailPresenter;
import com.tencent.miniproject.ui.i.IDetailActivity;
import com.tencent.miniproject.utils.DateUtil;
import com.tencent.miniproject.utils.ProgressDialogUtil;
import com.tencent.miniproject.view.NoScrollLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;


public class DetailActivity extends BaseActivity implements IDetailActivity {
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.iv_img)
    ImageView mIvImg;
    @BindView(R.id.civ_user_img)
    CircleImageView mCivUserImg;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;
    @BindView(R.id.tv_store)
    TextView mTvStore;
    @BindView(R.id.tv_cost)
    TextView mTvCost;
    @BindView(R.id.tv_location)
    TextView mTvLocation;
    @BindView(R.id.tv_distance)
    TextView mTvDistance;
    @BindView(R.id.tv_type)
    TextView mTvType;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.tv_content)
    TextView mTvContent;
    @BindView(R.id.tv_votes)
    TextView mTvVotes;
    @BindView(R.id.tv_comments)
    TextView mTvComments;
    @BindView(R.id.rv_comment)
    RecyclerView mRvComment;
    @BindView(R.id.sv_content)
    ScrollView mSvContent;
    @BindView(R.id.iv_vote)
    ImageView mIvVote;
    @BindView(R.id.et_comment)
    EditText mEtComment;

    private DetailCommentsAdapter mCommentsAdapter;
    private List<DetailComment> mList;
    private ContentDetail mContentDetail;
    private Integer mDetailId;  //动态ID
    private Double mLongitude;  //经度
    private Double mLatitude;  //纬度
    private IDetailPresenter mPresenter;

    @Override
    protected int setLayoutResID() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initData() {
        mDetailId = getIntent().getIntExtra("id", 0);
        mLongitude = getIntent().getDoubleExtra("longitude", 0);
        mLatitude = getIntent().getDoubleExtra("latitude", 0);
        mList = new ArrayList<>();
        mCommentsAdapter = new DetailCommentsAdapter(this);
    }

    @Override
    protected void initView() {
        NoScrollLayoutManager layoutManager = new NoScrollLayoutManager(this);
        layoutManager.setScrollEnabled(false);
        mRvComment.setLayoutManager(layoutManager);
        mRvComment.setItemAnimator(new DefaultItemAnimator());
        mRvComment.setAdapter(mCommentsAdapter);
    }

    @Override
    protected void initListener() {
        mPresenter = new DetailPresenter(this);
        mPresenter.getDetail(mDetailId, mLongitude, mLatitude);
        ProgressDialogUtil.showProgressDialog(this, "正在拼命加载中...");
        mEtComment.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND) {
                    String comment = mEtComment.getText().toString();
                    mPresenter.postComment(mContentDetail.getId(),comment);
                    ProgressDialogUtil.showProgressDialog(DetailActivity.this,"正在发表评论中...");
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void getDetailSuccess(ContentDetail contentDetail) {
        ProgressDialogUtil.dismiss();
        mContentDetail = contentDetail;
        mList = mContentDetail.getCommentList();
        Glide.with(this).load(mContentDetail.getContentUrl()).into(mIvImg);
        Glide.with(this).load(mContentDetail.getHeadUrl()).into(mCivUserImg);
        mTvStore.setText(mContentDetail.getStore());
        mTvLocation.setText(mContentDetail.getLocation());
        mTvCost.setText("￥" + mContentDetail.getCost()+"");
        mTvDistance.setText(mContentDetail.getDistance() + "米");
        mTvType.setText(mContentDetail.getImgLabel());
        mTvContent.setText(mContentDetail.getContent());
        mTvTime.setText(DateUtil.dateFormatDay(mContentDetail.getReleaseTime()));
        mTvVotes.setText(mContentDetail.getVotes()+"");
        mTvComments.setText(mContentDetail.getComments()+"");
        if (contentDetail.getVote()) {
            mIvVote.setImageDrawable(getResources().getDrawable(R.drawable.iv_good));
        } else {
            mIvVote.setImageDrawable(getResources().getDrawable(R.drawable.iv_good_blank));
        }
        mCommentsAdapter.updateData(mList);
    }

    @Override
    public void getDetailError(String msg) {
        ProgressDialogUtil.dismiss();
        showToast(msg);
    }

    @OnClick({R.id.iv_back, R.id.iv_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.iv_img:
                List<String> imgList = new ArrayList<>();
                imgList.add(mContentDetail.getContentUrl());
                Intent intent = DetailsPhotoActivity.newIntent(DetailActivity.this, 0, (ArrayList<String>) imgList);
                startActivity(intent);
                break;
        }
    }

    @OnClick(R.id.iv_vote)
    public void onViewClicked() {
        if (mContentDetail.getVote()) {
            mPresenter.onClickVote(mContentDetail.getId(), false);
        } else {
            mPresenter.onClickVote(mContentDetail.getId(), true);
        }
    }

    @Override
    public void onClickVoteResult(boolean isSuccess) {
        if (isSuccess) {
            if (mContentDetail.getVote()) {
                mIvVote.setImageDrawable(getResources().getDrawable(R.drawable.iv_good_blank));
                mContentDetail.setVote(false);
                mContentDetail.setVotes(mContentDetail.getVotes()-1);
                mTvVotes.setText(mContentDetail.getVotes()+"");
            } else {
                mIvVote.setImageDrawable(getResources().getDrawable(R.drawable.iv_good));
                mContentDetail.setVote(true);
                mContentDetail.setVotes(mContentDetail.getVotes()+1);
                mTvVotes.setText(mContentDetail.getVotes()+"");
            }
        }
    }

    @Override
    public void postCommentResult(boolean isSuccess) {
        if (isSuccess){
            mEtComment.setText("");
            mPresenter.getDetail(mDetailId,mLongitude,mLatitude);
        }else {
            ProgressDialogUtil.dismiss();
            showToast("出错啦！");
        }

    }
}
